package com.cjl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjl.anno.OperLog;
import com.cjl.auth.service.SysDeptService;
import com.cjl.commons.result.Result;
import com.cjl.commons.utils.MD5;
import com.cjl.model.system.SysDept;
import com.cjl.model.system.SysUser;
import com.cjl.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author cjl
 * @since 2023-03-19
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysDept")
@CrossOrigin
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    //条件分页查询
    @ApiOperation("部门条件分页查询")
    @GetMapping("findNodes")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    @OperLog(businessType = "SELECT", title = "部门管理")
    public Result index(){
        List<SysDept> list = sysDeptService.getDeptList();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加部门")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    @OperLog(businessType = "INSERT", title = "部门管理")
    public Result save(@RequestBody SysDept sysDept) {
        sysDeptService.save(sysDept);
        return Result.ok();
    }

    @ApiOperation(value = "更新部门")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @OperLog(businessType = "UPDATE", title = "部门管理")
    public Result updateById(@RequestBody SysDept sysDept) {
        sysDeptService.updateById(sysDept);
        return Result.ok();
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    @OperLog(businessType = "DELETE", title = "部门管理")
    public Result remove(@PathVariable Long id) {
        sysDeptService.removeDeptById(id);
        return Result.ok();
    }
    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @OperLog(businessType = "UPDATE", title = "部门管理")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        sysDeptService.updateStatus(id, status);
        return Result.ok();
    }

    @ApiOperation(value = "获取部门")
    @GetMapping("/toAssign/{userId}")
    @OperLog(businessType = "LIST", title = "部门管理")
    public Result toAssign(@PathVariable("userId") Long userId){
        Map<String, Object> map = sysDeptService.findDeptByUserId(userId);
        return Result.ok(map);
    }
}

